<?php  
class ShopTag{
	/**
	 * 模版标签
	 * @var array
	 */
	public $tag=array(

		//读取商品		//块级标签     支持嵌套4层
		'pdtlist'=>array('block'=>1,'leave'=>4),

		//读取栏目
		'channel'=>array('block'=>1,'leave'=>4),

		//分页列表数据
		'pagelist'=>array('block'=>1,'leave'=>4),

		//显示页码
		'pageshow'=>array('block'=>0,'leave'=>4)

	);

	/**
	 * 读取栏目
	 * @param  [type] $attr    [description]
	 * @param  [type] $content [description]
	 * @return [type]          [description]
	 */
	public function _channel($attr,$content){

		$cid = isset($attr['cid']) ? $attr['cid'] : '';

		//默认同级
		$type = isset($attr['type']) ? $attr['type'] : 'self';

		

		$php=<<<php

		<?php

		\$cid=$cid;

		\$type='$type';

		\$db=M('category');

		switch (\$type) {
			case 'self'://同级

				\$cat=\$db->find(\$cid);

				\$data=\$db->where("pid=".\$cat['pid'])->all();

				break;
			
			case 'son'://子级

				\$data=\$db->where("pid=".\$cid)->all();

				break;
		}
					
		foreach (\$data as \$d) :
			
			\$d['caturl']= U("Index/Index/category",array('cid'=>\$d['cid']));

		?>
php;

	$php.=$content;

	$php.="<?php endforeach; ?>";

	return $php;

	}

	/**
	 * 显示分页数据
	 * @param  [type] $attr    [description]
	 * @param  [type] $content [description]
	 * @return [type]          [description]
	 */
	public function pagelist($attr,$content){

		$row=isset($attr['row']) ? $attr['row'] : 10;

		$titlelen=isset($attr['titlelen']) ? $attr['titlelen'] : 20;

		$php=<<<php
		
		\$cid=Q('cid',0,'intval');

		\$cat=Data::channelList(M('category')->all(),\$cid);

		\$cat=array_keys(\$cat);

		\$cat[]=\$cid;

		\$db=K('Goods');

		\$where="cid IN(".implode(',', \$cat).")";

		\$page=new Page(\$db->where(\$where)->count(),$row);

		\$data=\$db->where(\$where)->limit(\$page->limit())->all();

		foreach(\$data as \$d):

			\$d['url']=U("Index/Index/content",array('gid'=>\$d['gid']));

			\$d['title']=mb_substr(\$d['title'],0,$titlelen,'utf-8');

		?>
php;

	$php.=$content;

	$php.="<?php endforeach; ?>";

	return $php;
	}

	/**
	 * 显示页码
	 * @param  [type] $attr    [description]
	 * @param  [type] $content [description]
	 * @return [type]          [description]
	 */
	public function _pageshow($attr,$content){

		return "<?php echo \$page->show(); ?>";
	}
}
?>